<?php

defined('BASEPATH') OR exit('No direct script access allowed');

class Member extends Base_Controller {

    public function index() {
        $department_id = $this->session->department_id;
        $where = "is_verify=1 AND department_id=$department_id";
        if($this->session->level==99){
            $where = 'is_verify=1';
        }
        $this->data['sumRecords'] = $this->member_model->getSum('su_user',$where);
        $this->load->view('public/header',$this->data);
        $this->load->view('pages/member');
    }

    public function verify() {
        $department_id = $this->session->department_id;
        $where = "is_verify=0 AND department_id=$department_id";
        if($this->session->level==99){
            $where = 'is_verify=0';
        }
        $this->data['sumRecords'] = $this->member_model->getSum('su_user',$where);
        $this->load->view('public/header',$this->data);
        $this->load->view('pages/verify');
    }

    public function manage() {
        $where = 'level<99 AND is_verify>0';
        $this->data['sumRecords'] = $this->member_model->getSum('su_user',$where);
        $this->load->view('public/header',$this->data);
        $this->load->view('pages/manage');
    }
    /*
     * 获取用户列表
     */
    public function get_member_list(){
        $redata = $this->httpcode;
        $page = $this->input->get('page');
        $limit = $this->input->get('limit');
        $page = $page * $limit;
        $department_id = $this->session->department_id;//获取session中的部门id
        $level = $this->session->level;//获取session中的用户等级
        $member = $this->member_model->memberInfo($department_id,$level,$page,$limit);
        if($member){
            $redata['errno'] = 666;
            $redata['error'] = '获取团队列表成功';
            $redata['memberList'] = $member;
        } else {
            $redata['errno'] = 601;
            $redata['error'] = '获取团队列表失败!';
        }
        echo json_encode($redata);

    }
    
    public function get_verify_list(){
        $redata = $this->httpcode;
        $department_id = $this->session->department_id;
        $level = $this->session->level;
        $page = $this->input->get('page');
        $limit = $this->input->get('limit');
        $page = $page * $limit;
        
        
        $verifylist = $this->member_model->verifyInfo($department_id,$level,$page,$limit);
        unset($verifylist['password']);
        if($verifylist){
            $redata['errno'] = 666;
            $redata['error'] = '获取待审核列表成功';
            $redata['verifylist'] = $verifylist;
        } else {
            $redata['errno'] = 602;
            $redata['error'] = '待审核列表为空';
        }
        echo json_encode($redata);
    }
    
    public function get_manage_list(){
        $redata = $this->httpcode;
        $level = $this->session->level;
        $managelist = $this->member_model->manageInfo($level);
        unset($managelist['password']);
        if($managelist){
            $redata['errno'] = 666;
            $redata['error'] = '获取用户列表成功';
            $redata['managelist'] = $managelist;
        } else {
            $redata['errno'] = 602;
            $redata['error'] = '待审核列表为空';
        }
        echo json_encode($redata);
    }
    
    public function verify_member(){
        $redata = $this->httpcode;
        $ids = $this->input->post('ids');
        $state = $this->input->post('state');
        $data = [
            'is_verify'=>$state
        ];
        $state = $this->member_model->verifyMember('su_user',$ids,$data);
        if ($state) {
            $redata['errno'] = 666;
            $redata['error'] =  '用户审核成功';
        } else {
            $redata['errno'] = 600;
            $redata['error'] = '用户审核失败';
        }
        echo json_encode($redata);
        
    }
    
    public function del_member(){
        $redata = $this->httpcode;
        $ids = $this->input->post('ids');
        $state = $this->member_model->delMember('su_user',$ids);
        if ($state) {
            $redata['errno'] = 666;
            $redata['error'] =  '删除用户成功';
        } else {
            $redata['errno'] = 600;
            $redata['error'] = '删除用户失败';
        }
        echo json_encode($redata);
        
    }
    public function blacklist(){
        $redata = $this->httpcode;
        $id = $this->input->post('id');
        $blacklist = $this->input->post('blacklist');
        $data = [
            'is_verify'=>$blacklist
        ];
        $state = $this->member_model->verifyMember('su_user',$id,$data);
        if ($state) {
            $redata['errno'] = 666;
            $redata['error'] =  '修改用户黑名单成功';
        } else {
            $redata['errno'] = 600;
            $redata['error'] = '修改用户黑名单失败';
        }
        echo json_encode($redata);
        
    }
    public function level(){
        $redata = $this->httpcode;
        $id = $this->input->post('id');
        $level = $this->input->post('level');
        $data = [
            'level'=>$level
        ];
        $state = $this->member_model->verifyMember('su_user',$id,$data);
        if ($state) {
            $redata['errno'] = 666;
            $redata['error'] =  '修改用户权限成功';
        } else {
            $redata['errno'] = 600;
            $redata['error'] = '修改用户权限失败';
        }
        echo json_encode($redata);
        
    }
    

}
